﻿using UnityEngine;

namespace UnityEditor
{
    [CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
    public class EnumFlagsAttributeDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            /*
         * 绘制多值枚举选择框,0 全部不选, -1 全部选中, 其他是枚举之和
         * 枚举值 = 当前下标值 ^ 2
         * 默认[0^2 = 1 , 1 ^2 = 2,  4, 16 , .....]
         */
            property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames);
            //Debug.Log("当前值:" + property.intValue);
        }
    }

    [CustomPropertyDrawer(typeof(RangeEvenAttribute))]
    public class RangeEvenDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            RangeEvenAttribute attribute = (RangeEvenAttribute)this.attribute;
            if (property.propertyType == SerializedPropertyType.Float)
                EditorGUI.Slider(position, property, attribute.min, attribute.max, label);
            else if (property.propertyType == SerializedPropertyType.Integer)
                EditorGUI.IntSlider(position, property, (int) attribute.min , (int) attribute.max, label);
            else
                EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
        }
    }

    [CustomPropertyDrawer(typeof(RangeUnevenAttribute))]
    public class RangeUnevenDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            RangeUnevenAttribute attribute = (RangeUnevenAttribute)this.attribute;
            if (property.propertyType == SerializedPropertyType.Float)
                EditorGUI.Slider(position, property, attribute.min, attribute.max, label);
            else if (property.propertyType == SerializedPropertyType.Integer)
                EditorGUI.IntSlider(position, property, (int) attribute.min, (int) attribute.max, label);
            else
                EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
        }
    }
}